package com.srwl.mytx.widget.chatRow;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.TextView;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.srwl.mytx.R;
import com.srwl.mytx.Constant;
import com.srwl.mytx.EaseConstant;

import java.io.File;

public class ChatRowFile extends BaseChatRow {

    protected TextView fileNameView;
    protected TextView fileSizeView;
    protected TextView fileStateView;

    private BroadcastReceiver broadcastReceiver;
    TextView percentageView;

    public ChatRowFile(View itemView) {
        super(itemView);
    }

    @Override
    protected void onInflateView() {

    }

    @Override
    protected void onFindViewById(View itemView) {
        fileNameView = itemView.findViewById(R.id.tv_file_name);
        fileSizeView = itemView.findViewById(R.id.tv_file_size);
        fileStateView = itemView.findViewById(R.id.tv_file_state);
        percentageView = itemView.findViewById(R.id.percentage);
    }


    @Override
    protected void onSetUpView() {

        String filePath = message.getContent();
        if (!isSendMessage) {
            File file = new File(filePath);
            if (file != null && file.exists()) {
                fileStateView.setText(R.string.Have_downloaded);
            } else {
                fileStateView.setText(R.string.Did_not_download);
            }
            return;
        }
        handleSendMessage();
    }

    /**
     * handle sending message
     */
    protected void handleSendMessage() {


        switch (message.getSendState()) {
            case Constant.IM_SEND_STATE_SENDING:
                registerBroadcastReceiver();
                percentageView.setVisibility(View.VISIBLE);
                break;
            case Constant.IM_SEND_STATE_SUCCESS:
            case Constant.IM_SEND_STATE_FAIL:
                percentageView.setVisibility(View.INVISIBLE);
                removeBroadcastReceiver();
                break;
            default:
                percentageView.setVisibility(View.INVISIBLE);
                break;
        }

    }


    @Override
    protected void onUpdateView() {

    }

    @Override
    protected void onBubbleClick() {


    }

    protected void registerBroadcastReceiver() {
        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();

                if (action.equals(EaseConstant.ACTION_SEND_MESSAGE_PROGRESS_CHANGE)) {
                    String msgId = intent.getStringExtra(EaseConstant.UPLOAD_MSG_ID);
                    if (msgId != message.getId()) {
                        return;
                    }
                    final String uploadProgress = intent.getIntExtra(EaseConstant.UPLOAD_PROGRESS,0) + "";
                    //语音消息 没有 percentageView，如果有必须，可以在语音消息的布局里面 增加 进度
                    if (uploadProgress != null && percentageView != null) {
                        percentageView.post(new Runnable() {
                            @Override
                            public void run() {
                                percentageView.setText(uploadProgress+"%");
                            }
                        });
                    }
                }
            }
        };
        IntentFilter filter = new IntentFilter();
        filter.addAction(EaseConstant.ACTION_SEND_MESSAGE_PROGRESS_CHANGE);
        LocalBroadcastManager.getInstance(context).registerReceiver(broadcastReceiver, filter);
    }

    protected void removeBroadcastReceiver() {
        if (broadcastReceiver != null) {
            broadcastReceiver = null;
            LocalBroadcastManager.getInstance(context).unregisterReceiver(broadcastReceiver);
        }
    }


}
